<!--
 * @Description: 用户新增服务订单页面
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-05-28 00:54:04
 * @LastEditors: Yuanjy
 * @LastEditTime: 2020-05-28 01:34:41
--> 
<template>
  <div class="inline">
    <el-button size="mini" type="success" @click="to">添加服务订单</el-button>
    <el-dialog :append-to-body="true" :visible.sync="dialog" title="新增服务订单" width="600px">
      <el-form ref="form" :model="form" size="small" label-width="100px">
        <el-form-item label="用户姓名" prop="delivery_name">
          <el-input v-model="form.delivery_name" style="width:80%" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" style="width:80%" />
        </el-form-item>
        <el-form-item label="来源渠道" prop="channel_code">
          <treeselect v-model="form.channel_code" :options="secondChannels" :flatten-search-results="true" :disable-branch-nodes="true" :show-count="true" style="width:80%" placeholder="请选择渠道" />
        </el-form-item>
        <el-form-item label="订单类型" prop="order_type">
          <el-select v-model="form.order_type" clearable placeholder="类型" class="filter-item" style="width:80%">
            <el-option v-for="(type, index) in types" :key="index" :label="type" :value="index"/>
          </el-select>
        </el-form-item>
        <el-form-item label="宝贝详情">
          <el-input v-model="form.goods_info" style="width:80%" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="dialog = false">取消</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: { Treeselect },
  props: {
    data: {
      type: Object,
      required: true
    },
    sup_this: {
      type: Object,
      required: true
    },
    userId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      secondChannels: [],
      types: [],
      form: {
        delivery_name: '',
        channel_code: null,
        phone: '',
        order_type: '',
        goods_info: '',
        external_user_id: 0,
      },
      formRules: {
        delivery_name: [
          { required: true, message: '请输入用户姓名', trigger: 'blur' }
        ],
        channel_code: [
          { required: true, message: '请选择渠道信息', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        order_type: [
          { required: true, message: '请选择订单类型', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    to() {
      this.dialog = true

      this.$api.order.service
        .params({
          params: { keys: ['types'] }
        })
        .then(res => {
          this.types = res.data.types
        }).catch(err => {
          console.log(err)
        })

      //查询渠道信息
      this.$api.enterprise.channel
        .cascadeSecond({
          params: { status: 1 }
        })
        .then(res => {
          this.secondChannels = res.data.channels
        }).catch(err => {
          console.log(err)
        })
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.external_user_id = this.userId
          this.$api.user.externalUser
            .addService({
              data: this.form
            })
            .then(res => {
              this.$notify({ title: '添加成功', type: 'success', duration: 2500 })
              this.loading = false
              this.dialog = false
            }).catch(err => {
              this.loading = false
              console.log(err)
            })
        } else {
          console.log('error')
          return false
        }
      })
    },
  }
}
</script>

<style scoped>
  div.inline {
    display: inline-block;
    margin-right: 3px;
  }
</style>
